﻿

apolloProjection = function (container, opts) {
    this.opts = opts || {};

    var that = this;

    this.domContainer = $(container);
    this.domDropDown1Container = null;
    this.domDropDown2Container = null;
    this.domLoadingContainer = null;
    this.loadedColumns = [];
    this.projection = this.opts.projection;
    this.baseEntityID = this.opts.entityID;
    this.domValueContainer = $(this.opts.valueContainer);
    this.onchange = this.opts.onchange || function () {
        if (that.domValueContainer)
            that.domValueContainer.val(that.projection);
    };

    this.draw();
}


apolloProjection.prototype.draw = function () {
    var that = this;

    // clear dom container
    this.domContainer.empty();

    // generate html code for dorpdowns and loading
    this.domDropDown1Container = $("<select>");
    this.domDropDown2Container = $("<select>");
    this.domLoadingContainer = $("<img src='/css/apollo/loading16x16.gif' border='0' alt='Loading' />");

    // add them to page
    this.domContainer.append(this.domDropDown1Container);
    this.domContainer.append(this.domDropDown2Container);
    this.domContainer.append(this.domLoadingContainer);

    this.domDropDown1Container.hide();
    this.domDropDown2Container.hide();

    // init the first dropdown
    this.populateDropDownFromAjax(null, this.domDropDown1Container);

    // handler dropdown events
    this.domDropDown1Container.change(function () {
        // check if the selected item is an object
        var selectedColumn = that.getSelectedColumn(that.domDropDown1Container.val());

        if (!selectedColumn)
            return;

        if (selectedColumn.fieldType == "object") {
            if (!selectedColumn.innerColumns) {
                that.populateDropDownFromAjax(selectedColumn, that.domDropDown2Container);
                that.domLoadingContainer.show();
                that.domDropDown2Container.hide();
            }
        } else {
            that.domDropDown2Container.hide();
            that.projection = selectedColumn.name;
            that.onchange();
        }

    });

    this.domDropDown2Container.change(function () {
        var realDOMDropDown1 = that.domDropDown1Container.get()[0];
        var currentSelectedOpt = $(realDOMDropDown1.options[realDOMDropDown1.selectedIndex]);

        // copy item to first dropdown
        that.domDropDown2Container.children().each(function () {
            var current = $(this);

            if (current.val() != "") {
                current.clone().insertAfter(currentSelectedOpt);
            }
        });

        that.domDropDown1Container.val(that.domDropDown2Container.val());

        that.domDropDown1Container.change();

        that.domDropDown2Container.hide();
    });
}


apolloProjection.prototype.populateDropDownFromAjax = function (destinationLoadedColumn, destinationDropDown) {
    destinationDropDown.hide();
    this.domLoadingContainer.show();

    // clear dropdown options
    destinationDropDown.empty();

    // add empty option to dropdown
    destinationDropDown.append($("<option value=''> </option>"));

    // get the rest of the columns
    var url = "/TypeDefinitionHandler.ashx?entityID=" + this.baseEntityID;

    if (destinationLoadedColumn)
        url += "&path=" + destinationLoadedColumn.name;

    var that = this;

    jQuery.ajax({
        url: url,
        dataType: "text",
        success: function (jsonData) {
            //alert(jsonData);

            var jsonObject = jQuery.parseJSON(jsonData);

            if (jsonObject.type == "Not found") {
                alert("Type for \"" + path + "\" not found!");
                return;
            }

            if (destinationLoadedColumn)
                destinationLoadedColumn.innerColumns = [];

            if (that.projection && !destinationLoadedColumn) {
                destinationDropDown.append($("<option>").val(that.projection).text(that.projection));
                destinationDropDown.val(that.projection);
            }

            // populate current dropdown and vars
            for (var index = 0; index < jsonObject.columns.length; index++) {
                var opt = $("<option>");
                opt.val(jsonObject.columns[index].name);
                opt.text(jsonObject.columns[index].displayName)

                destinationDropDown.append(opt);

                if (destinationLoadedColumn)
                    destinationLoadedColumn.innerColumns.push(jsonObject.columns[index]);
                else
                    that.loadedColumns.push(jsonObject.columns[index]);
            }


            destinationDropDown.show();
            that.domLoadingContainer.hide();
        }
    });
}

apolloProjection.prototype.getSelectedColumn = function (selectedPath) {
    for (var index = 0; index < this.loadedColumns.length; index++) {
        var selectedColumn = this.getSelectedColumnRecursive(this.loadedColumns[index], selectedPath);

        if (selectedColumn)
            return selectedColumn;
    }

    return null;
}

apolloProjection.prototype.getSelectedColumnRecursive = function (parentColumn, selectedPath) {
    if (parentColumn.name == selectedPath)
        return parentColumn;

    if (parentColumn.innerColumns) {
        for (var index = 0; index < parentColumn.innerColumns.length; index++) {
            var innerSelectedColumn = this.getSelectedColumnRecursive(parentColumn.innerColumns[index], selectedPath);

            if (innerSelectedColumn)
                return innerSelectedColumn;
        }
    }

    return null;
}
















